package com.dragon.mmochat.model.activity;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.dragon.mmochat.model.enumType.MmochatMoneyType;
import com.dragon.mmochat.model.enumType.MmochatPayLotteryState;
import com.dragon.mmochat.model.role.MmochatPlayer;
import com.dragon.mmochat.service.MmochatDao;
import com.dragon.mmochat.service.MmochatGmService;
import com.dragon.mmochat.util.MmochatUtil;

public class MmochatDaoActivity {
	private int id;
	private MmochatPayLotteryState state = MmochatPayLotteryState.活动结束;
	private String Info; // 活动说明
	private String startMsg;// 开始系统消息
	private MmochatMoneyType moneyType;// 礼物类型
	private List<Integer> moneyAwards = new ArrayList<Integer>(); // 礼物数量

	// 活动期间，所有刷道过的用户:roleId-->用户信息
	private Map<Integer, MmochatDaoRole> payRoles = new ConcurrentHashMap<Integer, MmochatDaoRole>();

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public MmochatPayLotteryState getState() {
		return state;
	}

	public void setState(MmochatPayLotteryState state) {
		this.state = state;
	}

	public String getInfo() {
		return Info;
	}

	public void setInfo(String info) {
		Info = info;
	}

	public String getStartMsg() {
		return startMsg;
	}

	public void setStartMsg(String startMsg) {
		this.startMsg = startMsg;
	}

	public MmochatMoneyType getMoneyType() {
		return moneyType;
	}

	public void setMoneyType(MmochatMoneyType moneyType) {
		this.moneyType = moneyType;
	}

	public List<Integer> getMoneyAwards() {
		return moneyAwards;
	}

	public void setMoneyAwards(List<Integer> moneyAwards) {
		this.moneyAwards = moneyAwards;
	}

	public Map<Integer, MmochatDaoRole> getPayRoles() {
		return payRoles;
	}

	public void setPayRoles(Map<Integer, MmochatDaoRole> payRoles) {
		this.payRoles = payRoles;
	}

	// 获取我的道行
	public int getMyDao(MmochatPlayer me) {
		MmochatDaoRole info = payRoles.get(me.getRoleId());
		if (info == null) {
			return 0;
		}
		return info.getDao();
	}

	// 增加道行记录
	public void addDao(MmochatPlayer me, int add) {
		MmochatDaoRole info = payRoles.get(me.getRoleId());
		if (info == null) {
			// 刚参加活动
			info = new MmochatDaoRole();
			info.setRoleId(me.getRoleId());
			info.setSkyId(me.getSkyId());
			info.setName(me.getName());
			payRoles.put(me.getRoleId(), info);
		}
		int curDao = info.getDao();
		curDao += add;
		info.setDao(curDao);
	}

	// 获取前N名名单
	public List<MmochatDaoRole> getTop() {
		if (moneyAwards != null && moneyAwards.size() > 0) {
			int count = moneyAwards.size();
			List<MmochatDaoRole> list = new ArrayList<MmochatDaoRole>(payRoles
					.values());
			Collections.sort(list);
			count = Math.min(count, list.size());
			return list.subList(0, count);
		}
		return new ArrayList<MmochatDaoRole>();
	}

	// 进行发奖
	public void doAward() {
		List<MmochatDaoRole> top = getTop();
		for (int i = 0; i < top.size(); i++) {
			MmochatDaoRole role = top.get(i);
			int moneyAward = moneyAwards.get(i);

			// 保存数据库
			try {
				MmochatDao.addNewActivityRecord2(role.getSkyId(), role
						.getRoleId(), moneyAward, id, role.getName());
			} catch (Exception e) {
				e.printStackTrace();
				return;
			}

			// 发送客服消息
			String userInfo = "恭喜您在本次刷道比赛中，取得"
					+ MmochatUtil.wrapColor("第" + (i + 1) + "名", Color.green)
					+ "，获得奖励"
					+ MmochatUtil.wrapColor(moneyAward + moneyType.toString(),
							Color.red);
			if (moneyType == MmochatMoneyType.K币) {
				userInfo += "(K币奖励将在7个工作日内统一发放)!";
			} else if (moneyType == MmochatMoneyType.人民币) {
				userInfo += "(现金奖励由客服发放，请注意客服消息通知)!";
			} else {
				userInfo += "(奖励已发放给您，请查收)!";
			}
			userInfo = MmochatUtil.wrapColor(userInfo, Color.yellow);
			MmochatGmService.sendKufuPrivateMsg(role.getRoleId(), userInfo,
					false);

			// 如果是金钱或元宝，则马上下发奖励(不能大于10000000，防止数据异常)
			int moneyCount = moneyAward;
			if (moneyCount > 0 && moneyCount < 10000000) {
				switch (moneyType) {
				case 元宝: {
					MmochatPlayer me = MmochatGmService.getRoleInfoById(role
							.getRoleId());
					me.addBig_money(moneyCount);
					try {
						MmochatDao.addRoleBigMoney(me.getRoleId(), moneyCount);
					} catch (Exception e) {
						e.printStackTrace();
					}
					break;
				}
				case 金钱: {
					MmochatPlayer me = MmochatGmService.getRoleInfoById(role
							.getRoleId());
					try {
						me.addAndUpdateSmall_money(moneyCount);
					} catch (Exception e1) {
						e1.printStackTrace();
					}
					try {
						MmochatDao
								.addRoleSmallMoney(me.getRoleId(), moneyCount);
					} catch (Exception e) {
						e.printStackTrace();
					}
					break;
				}
				default:
					break;
				}
			}

		}

	}

}
